package ptij.ADSL.de;

import ptolemy.kernel.util.*;
import ptolemy.actor.*;
import ptolemy.actor.gui.*;
import ptolemy.data.expr.*;
import ptolemy.data.*;
import ptolemy.domains.de.kernel.*;

import ptolemy.domains.de.lib.TimedDelay;
import ptolemy.actor.lib.FileWriter;

import ptij.ADSL.common.NullO;
import ptij.ADSL.common.Timer;

public class initTest {
            
    public static void main(String [] arg) throws IllegalActionException,
        NameDuplicationException {
	
	int toATURMode = 1;
	int toATUCMode = 1;
	
        TypedCompositeActor _toplevel = new TypedCompositeActor();
        _toplevel.setName("toplevel");

        DEDirector deDirector = new DEDirector(_toplevel, "deDirector");
	deDirector.stopTime.setExpression("200000.0");
	    	    
	Manager exec = new Manager("exec");
	_toplevel.setManager(exec);

        ptij.ADSL.de.ATUC.Initializer initATUC = new
	        ptij.ADSL.de.ATUC.Initializer(_toplevel, "initATUC", 
		toATUCMode, toATURMode);

        ptij.ADSL.de.ATUR.Initializer initATUR = new
	        ptij.ADSL.de.ATUR.Initializer(_toplevel, "initATUR",
		toATURMode, toATUCMode);
		
	NullO no1 = new NullO(_toplevel, "no1");
	NullO no2 = new NullO(_toplevel, "no2");
	    
	Timer t1 = new Timer(_toplevel, "t1");
	Timer t2 = new Timer(_toplevel, "t2");
	    	    
	_toplevel.connect(no1.output, initATUC.reset);
	_toplevel.connect(no2.output, initATUR.reset);

        if(toATURMode == 0) {
	    for(int i = 0;i < 255;i++)
	        _toplevel.connect(initATUC.toATUR[i], initATUR.fromATUC[i]);
	}
	else {
	    _toplevel.connect(initATUC.toATUR[0], initATUR.fromATUC[0]);
	}
	 
	if(toATUCMode == 0) {   
	    TimedDelay d1[] = new TimedDelay[31];	    
	    for(int i = 0;i < 31;i++) {
	        d1[i] = new TimedDelay(_toplevel, "d1_" + i);
	        _toplevel.connect(initATUR.toATUC[i], d1[i].input);
	        _toplevel.connect(d1[i].output, initATUC.fromATUR[i]);
	    }
	}
	else {
	    TimedDelay d1 = new TimedDelay(_toplevel, "d1");
	    _toplevel.connect(initATUR.toATUC[0], d1.input);
	    _toplevel.connect(d1.output, initATUC.fromATUR[0]);
	}
	    
	TimedDelay d2 = new TimedDelay(_toplevel, "d2");
	TimedDelay d3 = new TimedDelay(_toplevel, "d3");
	    
	_toplevel.connect(initATUC.startTimer, t1.startTimer);
	_toplevel.connect(initATUC.stopTimer, t1.stopTimer);
	_toplevel.connect(t1.timerEvent, d2.input);
	_toplevel.connect(d2.output, initATUC.timerEvent);
	    
	_toplevel.connect(initATUR.startTimer, t2.startTimer);
	_toplevel.connect(initATUR.stopTimer, t2.stopTimer);
	_toplevel.connect(t2.timerEvent, d3.input);
	_toplevel.connect(d3.output, initATUR.timerEvent);
	    
	FileWriter f1 = new FileWriter(_toplevel, "fw1");
	_toplevel.connect(initATUC.start, f1.input);
	_toplevel.connect(initATUC.idftMode, f1.input);
	_toplevel.connect(initATUC.dftMode, f1.input);

	FileWriter f2 = new FileWriter(_toplevel, "fw2");
	_toplevel.connect(initATUR.start, f2.input);
	_toplevel.connect(initATUR.idftMode, f2.input);
	_toplevel.connect(initATUR.dftMode, f2.input);
	    
        exec.run();
        return;
    }
}
